const app = getApp();
Page({
  data:{
    balance:"0.00",
    authorization: app.globalData.authorization,
    list:[],
    moneyin: '',
    moneyout:'',
    putForward :'',
    // ok:false,
    //用户个人信息
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    isTeamhaving:false,
    baseInfo:{}
  },
  toScan:function(){
    wx.navigateTo({
      url: '/pages/lotsize/lotsize',
    })
  },
  onShow:function(){
    // console.log(options.phone);
    wx.removeStorage({
      key: 'contact',
      success: function (res) {
      }
    })
    this.getInfo();
    this.getSummary();
    this.isTeamTranster();
  },
  isTeamTranster:function(){
    const _this = this;
    //判断是否有团队提现
    wx.request({
      url: app.globalData.path + 'services/app/applets/TeamTransferSetting',
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          if(res.data.result){
            _this.setData({
              isTeamhaving: true
            })
          }else{
            _this.setData({
              isTeamhaving: false
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 基本信息
  getInfo:function(){
    const _this = this;
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        // console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        _this.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
        app.globalData.userInfo = res.userInfo
      }
    })
    // console.log('=====================');
    // console.log(app.globalData.userInfo.avatarUrl);
    // 用户信息
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetCurrentLoginUserInfo',
      data: {
        profilePicturePath: app.globalData.userInfo.avatarUrl
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.statusCode == 401) {
          wx.removeStorage({
            key: 'token',
            success: function (res) {
            },
          })
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
        if (res.data.success) {
          app.globalData.baseInfo = res.data.result;
          _this.setData({
            balance: _this.numFilter(res.data.result.balance)
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    // 账单列表
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBills',
      data: {
        pageSize: 10,
        pageIndex: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.statusCode == 401) {
          wx.removeStorage({
            key: 'token',
            success: function (res) {
            },
          })
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
        if (res.data.success) {
          let list = res.data.result.items;
          list.forEach(item => {
            // item.creationTime = item.creationTime.substr(0, 10);
            item.money = _this.numFilter(item.money);
            item.balance = _this.numFilter(item.balance);
            item.creationTime = _this.handleDate(item.creationTime)
          })
          if (list.length > 10) {
            let arr10 = [];
            for (let i = 0; i < 10; i++) {
              arr10.push(list[i])
            }
            _this.setData({
              list: arr10
            })
          } else {
            _this.setData({
              list: list
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 当月汇总
  getSummary:function(){
    let _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetSummaryByMonth',
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            moneyin: _this.numFilter(res.data.result.in),
            moneyout: _this.numFilter(res.data.result.out),
            putForward: _this.numFilter(res.data.result.putForward)
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //提现
  toTransferme:function(){
    wx.navigateTo({
      url: '../transferme/transferme',
    })
  },
  // 转账
  toTransferother: function () {
    wx.navigateTo({
      url: '../transferother/transferother',
    })
  },
  // 转账
  toAllcash: function () {
    wx.navigateTo({
      url: "/pages/allcash/allcash",
    })
  },
  // 打款
  toWithdraw: function () {
    wx.navigateTo({
      url: '../withdraw/withdraw',
    })
  },
  // toBill
  toBill(){
    wx.switchTab({
      url: '/pages/bill/bill',
    })
  },
  // 过滤器
  numFilter: function (val) {
    // 截取当前数据到小数点后两位
    // let realVal = Number(val).toFixed(2)
    // num.toFixed(2)获取的是字符串
    // return Number(realVal)
    if (val || val == 0) {
      val = val.toString().replace(/\$|\,/g, '');
      if (isNaN(val)) {
        val = "0";
      }
      let sign = (val == (val = Math.abs(val)));
      val = Math.floor(val * 100 + 0.50000000001);
      let cents = val % 100;
      val = Math.floor(val / 100).toString();
      if (cents < 10) {
        cents = "0" + cents
      }
      for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
        val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3));
      }
      return (((sign) ? '' : '-') + val + '.' + cents);
    }
  },
  // 上拉刷新
  onPullDownRefresh() {
    　console.log('--------下拉刷新-------')
      const _this = this;
    　wx.showNavigationBarLoading() //在标题栏中显示加载
      // 用户信息
      wx.request({
       url: app.globalData.path + 'services/app/applets/GetCurrentLoginUserInfo',
       data: {},
       method: 'POST',
       header: {
         'content-type': 'application/json', // 默认值
         'Authorization': 'Bearer ' + app.globalData.authorization
       },
       success: function (res) {
         console.log(res.data);
         if (res.statusCode == 401) {
           wx.removeStorage({
             key: 'token',
             success: function (res) {
             },
           })
           wx.reLaunch({
             url: '/pages/login/login'
           })
         }
         if (res.data.success) {
           app.globalData.baseInfo = res.data.result;
           _this.setData({
             balance: _this.numFilter(res.data.result.balance)
           })
           // 账单列表
           wx.request({
             url: app.globalData.path + 'services/app/applets/GetBills',
             data: {
               pageSize: 10,
               pageIndex: 1
             },
             method: 'POST',
             header: {
               'content-type': 'application/json', // 默认值
               'Authorization': 'Bearer ' + app.globalData.authorization
             },
             success: function (res) {
               console.log(res.data);
               if (res.statusCode == 401) {
                 wx.removeStorage({
                   key: 'token',
                   success: function (res) {
                   },
                 })
                 wx.reLaunch({
                   url: '/pages/login/login'
                 })
               }
               if (res.data.success) {
                 let list = res.data.result.items;
                 list.forEach(item => {
                   // item.creationTime = item.creationTime.substr(0, 10);
                   item.money = _this.numFilter(item.money);
                   item.balance = _this.numFilter(item.balance);
                   item.creationTime = _this.handleDate(item.creationTime)
                 })
                 if (list.length > 10) {
                   let arr10 = [];
                   for (let i = 0; i < 10; i++) {
                     arr10.push(list[i])
                   }
                   _this.setData({
                     list: arr10
                   })
                 } else {
                   _this.setData({
                     list: list
                   })
                 }
               } else {
                 wx.showToast({
                   title: res.data.error.message,
                   icon: 'none',
                   duration: 2000
                 })
               }
             },
             complete:function(res){
               wx.hideNavigationBarLoading() //完成停止加载
               wx.stopPullDownRefresh() //停止下拉刷新
             }
           })
         } else {
           wx.showToast({
             title: res.data.error.message,
             icon: 'none',
             duration: 2000
           })
         }
       }
     })
  },
  // 处理日期
  handleDate: function (val) {
    let a1 = val.split(' ')[0];
    let a2 = val.split(' ')[1];
    a1 = a1.substring(5, 10);
    a2 = a2.substring(0,5);
    // console.log(a1);
    // console.log(a2);
    if (this.getMD() == a1) {
      return '今天 ' + a2;
    } else if (this.getYe() == a1) {
      return '昨天 ' + a2;
    } else {
      return a1 + ' ' + a2;
    }
  },
  // 今天
  getMD: function () {
    var util = require('../../utils/util.js');
    var now = new Date();
    var T1 = util.formatTime(now);
    let a1 = T1.split(' ')[0];
    // a1.substring(5, 10);
    return a1.substring(5, 10);
  },
  // 昨天
  getYe: function () {
    var util = require('../../utils/util.js');
    var now = new Date();
    var date = new Date(now.getTime() - 1 * 24 * 3600 * 1000);
    var T1 = util.formatTime(date);
    let a1 = T1.split(' ')[0];
    // a1.substring(5, 10);
    return a1.substring(5, 10);
  },
})